﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tww.IBAN.IBANUtilities.Validation;

namespace IBANTester
{
    public partial class IBANTestFormular : Form
    {
        public IBANTestFormular()
        {
            InitializeComponent();
        }

        private void btnTestIBAN_Click( object sender, EventArgs e )
        {
            var validator = new Tww.IBAN.IBANUtilities.Validation.DefaultIBANValidator();
            var ibanConverter = new Tww.IBAN.IBANUtilities.Converter.DefaultIBANConverter();

            bool ok = validator.ValidateIBANString( this.txtIBAN.Text );
            if ( ok )
            {
                var ibanmodel = ibanConverter.ConvertToIBANModel( new Tww.IBAN.IBANUtilities.Parser.DefaultIBANParser(), this.txtIBAN.Text );
                lblFehler.Text = string.Format( "Die IBAN scheint gültig zu sein \n BLZ : {0} \n KTO : {1} \n Bereich : {2} \n Land : {3} \n Sonstige Info : {4}", ibanmodel.BankCode, ibanmodel.AccountNumber, ibanmodel.Branch, ibanmodel.Country, ibanmodel.Miscellaneous );
            }
            else
            {
                lblFehler.Text = validator.GetValidationConflict( this.txtIBAN.Text );
            }
        }

        private void btnIBANGenerieren_Click( object sender, EventArgs e )
        {
            var blz = this.txtBLZ.Text;
            var kto = this.txtKontoNummer.Text;

            var ibanConverter = new Tww.IBAN.IBANUtilities.Converter.DefaultIBANConverter();
            Tww.IBAN.IBANUtilities.Model.IBANModel model;

            // Schlechte Umsetzung aber ist eh nur eine oberflächliche Umsetzung
            try
            {
                model = new Tww.IBAN.IBANUtilities.Model.IBANModel( "nl", kto, blz );
            }
            catch ( ArgumentException argExc )
            {
                this.lblFehlerGenerate.Text = argExc.Message;
                return;
            }

            var iban = ibanConverter.ConvertToElectronicIBAN( model );
            var vali = new Tww.IBAN.IBANUtilities.Validation.DefaultIBANValidator();

            string ok = vali.GetValidationConflict( model );

            if ( ok == string.Empty )
            {
                this.lblFehlerGenerate.Text = iban;
                this.lblFehlerGenerate.Text += "\n" + ibanConverter.ConvertToFourBlockIBAN( model ); ;
            }
            else
            {
                this.lblFehlerGenerate.Text = ok;
            }

        }
    }
}
